package com.dqp.cslggroup.Widget;

import android.annotation.SuppressLint;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import com.dqp.cslggroup.R;
import com.dqp.cslggroup.SubjectView.MySubject;
import com.dqp.cslggroup.SubjectView.SubjectRepertory;
import com.dqp.cslggroup.UI.MyApplication;
import com.dqp.cslggroup.Util.GetTimeUtil;
import com.dqp.cslggroup.Util.TimeTools;
import com.dqp.cslggroup.bean.student;
import com.zhuangfei.timetable.model.Schedule;
import com.zhuangfei.timetable.model.ScheduleSupport;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MyRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
    static List<Schedule> toDayScheduleList=new ArrayList<>();
    private final Context mContext;
    private int mAppWidgetId;
    static List<MySubject> courseList = new ArrayList<>();
    private static String listItemClick = "com.dqp.cslggroup.MYAPPWIDGET_LISTVIEW_UPDATE";
    private static String buttonRefresh="com.dqp.cslggroup.MYAPPWIDGET_BUTTON_UPDATE";
    private int zhou,week;
    /*
     * 构造函数
     */
    MyRemoteViewsFactory(Context context, Intent intent) {
        mContext = context;
        mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

    }

    /*
     * MyRemoteViewsFactory调用时执行，这个方法执行时间超过20秒回报错。
     * 如果耗时长的任务应该在onDataSetChanged或者getViewAt中处理
     */
    @Override

    public void onCreate() {
        zhou=TimeTools.course_zhoushu(mContext);
        week=TimeTools.week();
        courseList = SubjectRepertory.loadSubjects();
        toDayScheduleList = ScheduleSupport.getHaveSubjectsWithDay(ScheduleSupport.transform(courseList), zhou,week);
    }

    /*
     * 当调用notifyAppWidgetViewDataChanged方法时，触发这个方法
     * 例如：MyRemoteViewsFactory.notifyAppWidgetViewDataChanged();
     */
    @Override
    public void onDataSetChanged() {
        zhou=TimeTools.course_zhoushu(mContext);
        week=TimeTools.week();
        courseList = SubjectRepertory.loadSubjects();
        toDayScheduleList = ScheduleSupport.getHaveSubjectsWithDay(ScheduleSupport.transform(courseList), zhou,week);
    }

    /*
     * 这个方法不用多说了把，这里写清理资源，释放内存的操作
     */
    @Override
    public void onDestroy() {
        toDayScheduleList.clear();
    }

    /*
     * 返回集合数量
     */
    @Override
    public int getCount() {
        return toDayScheduleList.size();
    }

    /*
     * 创建并且填充，在指定索引位置显示的View，这个和BaseAdapter的getView类似
     */
    @Override
    public RemoteViews getViewAt(int position) {
        if (position < 0 || position >= toDayScheduleList.size())
            return null;
        RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.course_widget_item);
        Schedule schedule=toDayScheduleList.get(position);
        String[] starttimes = new String[]{"08:00", "09:00", "10:10", "11:10", "13:30", "14:30", "15:40", "16:40", "18:30", "19:30","20:40","21:40","23:40"};
        String[] endtimes = new String[]{"08:50", "09:50", "11:00", "12:00", "14:20", "15:20", "16:30", "17:30", "19:20", "20:20","21:30","22:30","23:30"};
        int start = schedule.getStart() - 1;
        int step = schedule.getStep();
        rv.setTextViewText(R.id.course_widget_time, starttimes[start] + "\n" + endtimes[start + step -1]);
        rv.setTextViewText(R.id.course_widget_kcmc, schedule.getName());
        rv.setTextViewText(R.id.course_widget_teacher, schedule.getTeacher());
        rv.setTextViewText(R.id.course_widget_room, schedule.getRoom());
        Intent intent = new Intent();
        intent.putExtra(listItemClick, schedule.getName()+"\t"+starttimes[start] + "-" + endtimes[start + step -1]+"\t"+schedule.getRoom());
        rv.setOnClickFillInIntent(R.id.course_widget_item_layout, intent);
        return rv;
    }

    /*
     * 显示一个"加载"View。返回null的时候将使用默认的View
     */
    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    /*
     * 不同View定义的数量。默认为1（本人一直在使用默认值）
     */
    @Override
    public int getViewTypeCount() {
        return 1;
    }

    /*
     * 返回当前索引的。
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /*
     * 如果每个项提供的ID是稳定的，即她们不会在运行时改变，就返回true（没用过。。。）
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

}